package cn.tedu.charging.user.repository;

import cn.tedu.charging.common.enums.VehicleUserBindEnum;
import cn.tedu.charging.common.pojo.po.ChargingUserInfoPO;
import cn.tedu.charging.common.pojo.po.ChargingUserVehicleBindPO;
import cn.tedu.charging.common.pojo.po.ChargingVehiclePO;
import cn.tedu.charging.user.mapper.UserMapper;
import cn.tedu.charging.user.mapper.UserVehicleBindMapper;
import cn.tedu.charging.user.mapper.VehicleMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 处理的是车主数据读写
 * 为了数据解耦，特将持久层和mapper分开，单独作为仓储层
 */
@Repository
public class UserRepository {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Autowired
    private UserVehicleBindMapper userVehicleBindMapper;

    public ChargingUserInfoPO getUserByOpenId(String openId) {
        // 1. 调用mapper，查询数据库
        // select * from charging_user_info where wx_open_id = ?
        QueryWrapper<ChargingUserInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wx_open_id", openId);
        return userMapper.selectOne(queryWrapper);
    }

    public void saveUser(ChargingUserInfoPO po) {
        if (po.getId() == null) {
            userMapper.insert(po);
        } else {
            userMapper.updateById(po);
        }
    }

    public ChargingUserInfoPO getUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    public List<ChargingUserVehicleBindPO> getBindedVehicle(Integer userId, Integer state) {
        //select * from charging_user_vehicle_bind where user_id=? and state=?
        QueryWrapper<ChargingUserVehicleBindPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("state", state);
        return userVehicleBindMapper.selectList(queryWrapper);
    }

    public ChargingVehiclePO getVehicleById(Integer vehicleId) {
        return vehicleMapper.selectById(vehicleId);
    }

    public ChargingVehiclePO getVehicleByLicense(String license) {
        QueryWrapper<ChargingVehiclePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("license", license);
        return vehicleMapper.selectOne(queryWrapper);
    }

    public void saveVehicle(ChargingVehiclePO cvpo) {
        vehicleMapper.insert(cvpo);
    }

    public void deleteUserBindVehicle(Integer userId, Integer vehicleId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("vehicle_id", vehicleId);
        userVehicleBindMapper.delete(queryWrapper);
    }

    public void bindUserVehicle(ChargingUserVehicleBindPO cvbpo) {
        userVehicleBindMapper.insert(cvbpo);
    }

    public void unbindUserVehicle(ChargingUserVehicleBindPO cvbpo) {
        UpdateWrapper<ChargingUserVehicleBindPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",  cvbpo.getUserId());
        updateWrapper.eq("vehicle_id", cvbpo.getVehicleId());
        updateWrapper.set("state", VehicleUserBindEnum.UNBIND.getState());
        updateWrapper.set("unbind_time", new Date());
        userVehicleBindMapper.update(null, updateWrapper);
    }
}
